package com.example.c_serializable;

import java.io.*;
import java.util.ArrayList;

public class c_serializable {
    public static void main(String[] args) {
//        write();
        read();
    }

    public static void write() {
        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"))) {
            ArrayList<Person> lists = new ArrayList<>();
            Person p1 = new Person("ceshi", 12);
            Person p2 = new Person("ceshi1", 2);
            Person p3 = new Person("ceshi2", 1);
            lists.add(p1);
            lists.add(p2);
            lists.add(p3);
            oos.writeObject(lists);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void read() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));){
            ArrayList<Person> lists = (ArrayList<Person>) ois.readObject();
            for (Person list : lists) {
                System.out.println(list);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
